package org.xjtu.weteam.web.controller;

import io.swagger.models.auth.In;
import io.swagger.v3.core.util.Json;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.xjtu.weteam.common.JsonResponse;
import org.xjtu.weteam.model.domain.Task;
import org.xjtu.weteam.model.dto.ReNewSonDTO;
import org.xjtu.weteam.model.dto.StatusNumDTO;
import org.xjtu.weteam.model.dto.TaskDTO;
import org.xjtu.weteam.model.dto.TaskDateDTO;
import org.xjtu.weteam.service.TaskService;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Kevin
 */
@RestController
@RequestMapping("/api/task")
public class TaskController {

    private final Logger logger = LoggerFactory.getLogger(TaskController.class);

    @Autowired
    private TaskService taskService;

    @GetMapping("/taskSearchAll")
    @ResponseBody
    public JsonResponse taskSearchAll(Integer projectId) {
        return JsonResponse.success(taskService.taskSearchAll(projectId));
    }
    @GetMapping("/changeTaskStatus")
    @ResponseBody
    public JsonResponse changeTaskStatus(Integer taskId, String newStatus) {
        return JsonResponse.success(taskService.changeTaskStatus(taskId, newStatus));
    }


    //获得某个用户的全部任务
    @PostMapping("/taskSearchAllByUser")
    @ResponseBody
    public JsonResponse taskSearchAllByUser(@RequestHeader("X-Access-Token") String token, String status){
        List<TaskDTO> taskDTO =  taskService.taskSelectAllByUser(token, status);
//        if(taskDTO == null) return JsonResponse.failure("wrong status!");
//        if (status == null || !status.equals("active")) {
//            return JsonResponse.failure("Invalid status parameter");
//        }
        return JsonResponse.success(taskDTO);
    }

    @PostMapping("/getStatusNum")
    @ResponseBody
    public JsonResponse getStatusNum(@RequestHeader("X-Access-Token") String token){
        List<StatusNumDTO> statusNum= taskService.selectStatusNum(token);
        return JsonResponse.success(statusNum);
    }

    @GetMapping("/getAllTasksCanBeParent")
    @ResponseBody
    public JsonResponse getAllTasksCBParent(Integer projectid, Integer taskId){
        List<Task> tasks = taskService.selectAllParent(projectid, taskId);
        return JsonResponse.success(tasks);
    }
    @GetMapping("/getAllTasksCanBeSon")
    @ResponseBody
    public JsonResponse getAllTasksCBSon(Integer projectid, Integer taskId){
        List<Task> tasks = taskService.selectAllSon(projectid, taskId);
        return JsonResponse.success(tasks);
    }

    @PostMapping("/createTask")
    @ResponseBody
    public JsonResponse createTask(TaskDateDTO taskDateDTO){
//        if (taskDateDTO == null || taskDateDTO.getTaskName() == null || taskDateDTO.getTaskName().isEmpty()) {
//            return JsonResponse.failure("TaskName cannot be null or empty");
//        }
        Task task = new Task(taskDateDTO);
        Integer taskId = taskService.createTask(task);
        return JsonResponse.success(taskId);
    }

    @PostMapping("/handleSonTask")
    @ResponseBody
    public JsonResponse handleSonTask(@RequestBody ReNewSonDTO dto){
        ArrayList<Integer> sonsId = dto.getSonsId();
        Integer taskId = dto.getTaskId();
        for (Integer integer : sonsId) {
            Task task = taskService.getByItsId(integer);
            task.setPId(taskId);
            taskService.updateParent(task);
        }
        return JsonResponse.success(true);
    }

    @PostMapping("/getCurrentChild")
    public JsonResponse getCurrentChild(Integer taskId){
        List<Task> tasks = taskService.selectSons(taskId);
        return JsonResponse.success(tasks);
    }

    @PostMapping("/updateTask")
    @ResponseBody
    public JsonResponse updateTask(TaskDateDTO taskDateDTO){
        try {
            Task task = new Task(taskDateDTO);
            taskService.updateByItsId(task);
            return JsonResponse.success(true);
        } catch (Exception e) {
            // 这里可以更具体地捕获特定的异常，比如TaskNotFoundException
            return JsonResponse.failure("Task update failed: " + e.getMessage());
        }
    }

}
